# 把字典中值包含NC的放到最前面
nc_to_head = lambda dict1:dict(sorted(dict1.items(),key=lambda x:x[1].find('NC')+1,reverse=True))

def dealSampleSheet(samplesheet):
    # samplesheet (csv)格式
    '''
    [Header],,,,,,,
    IEMFileVersion,5,,,,,,
    Date,2020/7/30,,,,,,
    Workflow,GenerateFASTQ,,,,,,
    Application,NextSeq FASTQ Only,,,,,,
    Instrument Type,NextSeq/MiniSeq,,,,,,
    Assay,Nextera DNA,,,,,,
    Index Adapters,"Nextera Index Kit (24 Indexes, 96 Samples)",,,,,,
    Description,,,,,,,
    Chemistry,Default,,,,,,
    ,,,,,,,
    [Reads],,,,,,,
    75,,,,,,,
    ,,,,,,,
    [Settings],,,,,,,
    Adapter,CTGTCTCTTATACACATCT,,,,,,
    ,,,,,,,
    [Data],,,,,,,
    *Sample_ID,Sample_Name,Sample_Plate,Sample_Well,*I7_Index_ID,*index,Sample_Project,Description
    S216YM0360,,,,81,L1,,
    S216YM0361,,,,82,L1,,
    S216YM0362,,,,83,L1,,
    S216YM0363,,,,84,L1,,
    S216YM0364,,,,85,L1,,
    S216YM0365,,,,86,L1,,
    '''
    with open(samplesheet) as ssf:
        _samples = {}
        _lane = {}
        # 使用flag记录状态 当读到表头时开始存入数据
        flag = 0
        for i in ssf:
            if flag == 0:
                if i.startswith('Sample_ID'):
                    flag = 1
                    continue
                continue
            #skip BY samples
            if flag == 1:
                x = i.split(',')
                if x[0].find('BY')>-1:continue
                _samples[x[4]] = x[0]
                _lane[x[4]] = x[5].strip()
        # _samples=s_dict(_samples)
        _samples=nc_to_head(_samples)
        # 把I7_Index_ID对应的样本id存入字典（样本id带NC的放到前面） 把I7_Index_ID对应的lane index存入字典 并返回
        # _samples:{I7_Index_ID:sampleId}
        #_lane:{I7_Index_ID:laneIndex}
        return (_samples, _lane)